package pokerAI.PokerAI;
import java.io.*;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Stack;
import java.util.Random;
import java.lang.Math;
import java.lang.String;

import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;


class Player{
        protected String name;
        protected int id;
        protected int emotionalState;
        protected LinkedList<Card> holeCards;
        protected LinkedList<Hand> currentHands;
        protected int money;
        protected int currentBet;
        protected boolean hasFolded;
        protected boolean hasBet;
        protected boolean handUpdated;
        protected Hand bestHand;
        protected boolean hasActed;
        protected String playerAct;
        private String type;
        
        
        //UI R.id
        int card1Id;
        int card2Id;
        int statusId;
        int moneyId;
        int roleId;
        
        //UI Widgets
    	private ImageView PlayerCard1;
    	private ImageView PlayerCard2;
    	private TextView PlayerStatus;
    	private TextView PlayerMoney;
    	private ImageView PlayerRole;
        
        //Android R Integers
        private Integer[][] XMLIDs = {
                //Card 1
        		{
	                R.id.Player1Card1,
	                R.id.Player2Card1,
	                R.id.Player3Card1,
	                R.id.Player4Card1,
	                R.id.UserHandCard1
                },
                //Card 2
                {
                	R.id.Player1Card2,
                	R.id.Player2Card2,
                	R.id.Player3Card2,
                	R.id.Player4Card2,
                	R.id.UserHandCard2
                },
                //Status
                {
                	R.id.Player1Status,
                	R.id.Player2Status,
                	R.id.Player3Status,
                	R.id.Player4Status,
                	R.id.UserStatus
                },
                //Money
                {
                	R.id.Player1Money,
                	R.id.Player2Money,
                	R.id.Player3Money,
                	R.id.Player4Money,
                	R.id.UserMoney
                } ,
                //Role
                {
                	R.id.Player1Role,
                	R.id.Player2Role,
                	R.id.Player3Role,
                	R.id.Player4Role,
                	R.id.UserRole
                }  
        };
        
        public Player(int id){
        		this.id = id;
                this.name = "default";
                this.emotionalState = 5;
                this.holeCards = new LinkedList<Card>();
                this.money = 200;
                this.currentBet = 0;
                this.hasFolded = false;
                this.hasBet = false;
                handUpdated = false;
                hasActed = false;
                currentHands = new LinkedList<Hand>();
                
                //Find UI ID's
                this.initPlayerUI();
        }
       
        public Player( String n, int id ){
        		this.id = id;    
        		this.name = n;
                this.emotionalState = 5;
                this.holeCards = new LinkedList<Card>();
                this.money = 200;
                this.currentBet = 0;
                this.hasFolded = false;
                this.hasBet = false;
                currentHands = new LinkedList<Hand>();
                
                //Find UI ID's
                this.initPlayerUI();
         }
        
        public void initPlayerUI() {
        	this.setType("human");
            //UI R.id
            this.card1Id = this.XMLIDs[0][this.id ];
            this.card2Id = this.XMLIDs[1][this.id];
            this.statusId = this.XMLIDs[2][this.id];
            this.moneyId = this.XMLIDs[3][this.id];
            this.roleId = this.XMLIDs[4][this.id];
        	
        }
       
        public String act(String[] validActions, int lastCurrBet) throws IOException
        {
        	
        		//BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                String s = this.playerAct;
                int userInput = 0;
                int bet=0;
                System.out.println("Player " + this.name);
                System.out.println("Your cards are: " + toStringHoleCards());
                System.out.println("Current bet is: " + lastCurrBet);
                System.out.println("Current cash is: " + money);
                System.out.println("Choose an action");
                for(int i=0; i<validActions.length; i++)
                {
                        //Display valid actions                
                        System.out.println("  (" + i + ")" + validActions[i]);
                }
                //s = br.readLine();
                //userInput = Integer.parseInt(s);
                s ="call";
                if( validActions[userInput] == "bet" ){
                        System.out.println("How much would you like to bet");
                       // s = br.readLine();
                        bet = Integer.parseInt(s);
                        currentBet = bet;
                }
                if( validActions[userInput] == "raise"){
                        System.out.println("How much would you like to raise by");
                        //s = br.readLine();
                        bet = Integer.parseInt(s);
                        //// vincnet removed .. it was here twice
                        currentBet = bet + lastCurrBet;
                }
                if( validActions[userInput] == "call" )
                {
                        currentBet = lastCurrBet;
                }
                //Take user input as an int which represents which valid action they choose
                hasActed = true;
                return validActions[userInput];
                
        }
        private Hand calcBestHand()
        {
                Hand temp = currentHands.get(0);
                for(int i=1; i<currentHands.size(); i++)
                {
                        if (temp.compareTo(currentHands.get(i)) < 0)
                        {
                                temp = currentHands.get(i);
                        }
                }
                return temp;
        }
        public void calcPossibleHands(LinkedList<Card> commCards)
        {
                int numCommCards = commCards.size();
                currentHands.clear();
                if(commCards.size() < 3)
                {
                        return;
                }
                LinkedList<Card> myCards = new LinkedList<Card>();
                Card[] temp = new Card[5];
                for(int i=0; i<2; i++)
                {
                        myCards.add(holeCards.get(i));
                }
                for(int i=0; i<commCards.size(); i++)
                {
                        myCards.add(commCards.get(i));
                }              
                if(numCommCards >= 3)
                {
                        temp[0] = myCards.get(0);
                        temp[1] = myCards.get(1);
                        temp[2] = myCards.get(2);
                        temp[3] = myCards.get(3);
                        temp[4] = myCards.get(4);
                        currentHands.add(new Hand(temp));
                }
                if(numCommCards >= 4)
                {                      
                        temp[0] = myCards.get(0);
                        temp[1] = myCards.get(1);
                        temp[2] = myCards.get(2);
                        temp[3] = myCards.get(3);
                        temp[4] = myCards.get(5);
                        currentHands.add(new Hand(temp));
                       
                        temp[0] = myCards.get(0);
                        temp[1] = myCards.get(1);
                        temp[2] = myCards.get(2);
                        temp[3] = myCards.get(4);
                        temp[4] = myCards.get(5);
                        currentHands.add(new Hand(temp));
                       
                        temp[0] = myCards.get(0);
                        temp[1] = myCards.get(1);
                        temp[2] = myCards.get(3);
                        temp[3] = myCards.get(4);
                        temp[4] = myCards.get(5);
                        currentHands.add(new Hand(temp));
                       
                        temp[0] = myCards.get(0);
                        temp[1] = myCards.get(2);
                        temp[2] = myCards.get(3);
                        temp[3] = myCards.get(4);
                        temp[4] = myCards.get(5);
                        currentHands.add(new Hand(temp));
                       
                        temp[0] = myCards.get(1);
                        temp[1] = myCards.get(2);
                        temp[2] = myCards.get(3);
                        temp[3] = myCards.get(4);
                        temp[4] = myCards.get(5);
                        currentHands.add(new Hand(temp));
                }
                if(numCommCards >= 5)
                {
                        temp[0] = myCards.get(0);
                        temp[1] = myCards.get(1);
                        temp[2] = myCards.get(3);
                        temp[3] = myCards.get(5);
                        temp[4] = myCards.get(6);
                        currentHands.add(new Hand(temp));
                       
                        temp[0] = myCards.get(0);
                        temp[1] = myCards.get(1);
                        temp[2] = myCards.get(2);
                        temp[3] = myCards.get(3);
                        temp[4] = myCards.get(6);
                        currentHands.add(new Hand(temp));
                       
                        temp[0] = myCards.get(0);
                        temp[1] = myCards.get(1);
                        temp[2] = myCards.get(2);
                        temp[3] = myCards.get(4);
                        temp[4] = myCards.get(6);
                        currentHands.add(new Hand(temp));
                       
                        temp[0] = myCards.get(0);
                        temp[1] = myCards.get(1);
                        temp[2] = myCards.get(2);
                        temp[3] = myCards.get(5);
                        temp[4] = myCards.get(6);
                        currentHands.add(new Hand(temp));
                       
                        temp[0] = myCards.get(0);
                        temp[1] = myCards.get(1);
                        temp[2] = myCards.get(3);
                        temp[3] = myCards.get(4);
                        temp[4] = myCards.get(6);
                        currentHands.add(new Hand(temp));
                       
                        temp[0] = myCards.get(0);
                        temp[1] = myCards.get(1);
                        temp[2] = myCards.get(4);
                        temp[3] = myCards.get(5);
                        temp[4] = myCards.get(6);
                        currentHands.add(new Hand(temp));
                       
                        temp[0] = myCards.get(0);
                        temp[1] = myCards.get(2);
                        temp[2] = myCards.get(3);
                        temp[3] = myCards.get(5);
                        temp[4] = myCards.get(6);
                        currentHands.add(new Hand(temp));
                       
                        temp[0] = myCards.get(0);
                        temp[1] = myCards.get(2);
                        temp[2] = myCards.get(3);
                        temp[3] = myCards.get(4);
                        temp[4] = myCards.get(6);
                        currentHands.add(new Hand(temp));
                       
                        temp[0] = myCards.get(0);
                        temp[1] = myCards.get(2);
                        temp[2] = myCards.get(4);
                        temp[3] = myCards.get(5);
                        temp[4] = myCards.get(6);
                        currentHands.add(new Hand(temp));
                       
                        temp[0] = myCards.get(0);
                        temp[1] = myCards.get(3);
                        temp[2] = myCards.get(4);
                        temp[3] = myCards.get(5);
                        temp[4] = myCards.get(6);
                        currentHands.add(new Hand(temp));
                       
                        temp[0] = myCards.get(1);
                        temp[1] = myCards.get(2);
                        temp[2] = myCards.get(3);
                        temp[3] = myCards.get(4);
                        temp[4] = myCards.get(6);
                        currentHands.add(new Hand(temp));
                       
                        temp[0] = myCards.get(1);
                        temp[1] = myCards.get(2);
                        temp[2] = myCards.get(3);
                        temp[3] = myCards.get(5);
                        temp[4] = myCards.get(6);
                        currentHands.add(new Hand(temp));
                       
                        temp[0] = myCards.get(1);
                        temp[1] = myCards.get(2);
                        temp[2] = myCards.get(4);
                        temp[3] = myCards.get(5);
                        temp[4] = myCards.get(6);
                        currentHands.add(new Hand(temp));
                       
                        temp[0] = myCards.get(1);
                        temp[1] = myCards.get(3);
                        temp[2] = myCards.get(4);
                        temp[3] = myCards.get(5);
                        temp[4] = myCards.get(6);
                        currentHands.add(new Hand(temp));
                       
                        temp[0] = myCards.get(2);
                        temp[1] = myCards.get(3);
                        temp[2] = myCards.get(4);
                        temp[3] = myCards.get(5);
                        temp[4] = myCards.get(6);
                        currentHands.add(new Hand(temp));
                }
        }
        public void fold(){
                hasFolded = true;
                this.PlayerStatus.setText("Fold");
        }
        public int getBet()
        {
                int temp = currentBet;
                currentBet = 0;
                money -= temp;
                printMoney();
                return temp;
                
        }




		public Hand getCurrentBestHand()
        {
                if(handUpdated || bestHand == null)
                {
                        handUpdated = false;
                        bestHand = calcBestHand();
                        return bestHand;
                }
                else
                {
                        return bestHand;
                }
        }

        public LinkedList<Hand> getCurrentHands()
        {
                return currentHands;
        }
       
        public int getEmotionalState(){
                return emotionalState;
        }
       
        public boolean getHasBet(){
                return hasBet;
        }
       
        public boolean getHasFolded(){
                return hasFolded;
        }
       
        public LinkedList<Card> getHoleCards(){
                return holeCards;
        }
       
        public int getMoney(){
                return money;
        }
       
        public String getName(){
                return name;
        }
       
        public boolean hasActed()
        {
                return hasActed;
        }
       
        public void placeBet( int d ){
                if( d > money ){
                        System.out.println("You do not have that much money, all of your money is now betted");
                        d = money;
                }
                money = money - d;
                currentBet = d;
                hasBet = true;
        }

       
        public void receiveCard( Card c ){
                holeCards.add(c);
                handUpdated = true;
                
                
                this.PlayerCard1.setImageResource(R.drawable.back3);
                this.PlayerCard2.setImageResource(R.drawable.back3);
        		this.PlayerCard1.setVisibility(0);
        		this.PlayerCard2.setVisibility(0);
        }
        public void setBet(int b)
        {
                currentBet = b;
        }
       
        public void receiveMoney( int m ){
                money = money + m;
        }
       
        public void setFolded(boolean b){
                hasFolded = b;
        }
        public void setHasBet(boolean b){
                hasBet = b;
        }
        public String toStringHoleCards()
        {
                String toReturn = holeCards.get(0).toString();
                toReturn += " and ";
                toReturn += holeCards.get(1).toString();
                toReturn += " \n";
                return toReturn;
        }
        public void setNotActed()
        {
                hasActed = false;
        }

		public void setDealer() {
			// TODO Auto-generated method stub
			this.PlayerRole.setImageResource(R.drawable.dealerbutton);			
		}
		
		public void setBigBlind() {
			// TODO Auto-generated method stub
			this.PlayerRole.setImageResource(R.drawable.bigblind);			
		}
		public void smallBlind() {
			// TODO Auto-generated method stub
			this.PlayerRole.setImageResource(R.drawable.smallblind);			
		}

		public void setPlayerCard1(ImageView playerCard1) {
			PlayerCard1 = playerCard1;
		}

		public ImageView getPlayerCard1() {
			return PlayerCard1;
		}
		
		public void setPlayerCard2(ImageView playerCard2) {
			PlayerCard2 = playerCard2;
		}

		public ImageView getPlayerCard2() {
			return PlayerCard1;
		}

		public void setPlayerStatus(TextView playerStatus) {
			PlayerStatus = playerStatus;
		}

		public TextView getPlayerStatus() {
			return PlayerStatus;
		}

		public void setPlayerMoney(TextView playerMoney) {
			PlayerMoney = playerMoney;
		}

		public TextView getPlayerMoney() {
			return PlayerMoney;
		}

		public void setPlayerRole(ImageView playerRole) {
			PlayerRole = playerRole;
		}

		public ImageView getPlayerRole() {
			return PlayerRole;
		}

		public void setType(String type) {
			this.type = type;
		}

		public String getType() {
			return type;
		}
		
        public void printMoney() {
			// TODO Auto-generated method stub
        	//Subtract from money total        	
        	this.PlayerMoney.setText(String.valueOf(this.money));	
		}
        public void printStatus(String status) {
			// TODO Auto-generated method stub
        	//Subtract from money total
        	this.PlayerStatus.setText(status);
        	System.out.println(status);
        	if (status.compareToIgnoreCase("fold") == 0){
        		System.out.println("Hiding cards because of fold!");
        		
        		this.PlayerCard1.setVisibility(4);
        		this.PlayerCard2.setVisibility(4);
        	}
        		
		}
		

}
